<?php
namespace app\classes\helper;
use think\Db;

class AdvertBehavior
{
    /*记录订单广告数据*/
    public function writeOrderRecord($orderData,$oid)
    {

        $record = Db::table('quick_user_behavior_monitoring')
            ->where('channel_id',$orderData['channel_id'])
            ->where('uid',$orderData['uid'])
            ->where('type',$orderData['advert_media_id'])
            ->field('id,advertiser_id,campaign_id,aid,cid')->order('id DESC')->findOrEmpty();

        if ($record && strlen($record['advertiser_id']) > 1) {
            $data['order_id']       = $oid;
            $data['behavior_id']    = $record['id'];
            $data['adtype']         = $record['type'];
            $data['advertiser_id']  = $record['advertiser_id'];
            $data['aid']            = $record['aid'];
            $data['cid']            = $record['cid'];
            $data['campaign_id']    = $record['campaign_id'];
            $data['create_date']    = $orderData['create_date'];

            Db::table('pay_order_advert')->insert($data);
        }
    }

    /*记录广告行为*/
    public function writeRecord($uid,$channel_id)
    {
        $params['channel_id'] = $channel_id;

        $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';

        $usersign = md5(get_phone_brand($ua).'_'.request()->ip());

        Db::table('quick_user_behavior_monitoring')
            ->where('uid', 0)
            ->where('adsign',$usersign)->where('channel_id',$channel_id)
            ->force('usersign')->update(['uid' => $uid]);

        $behavior = Db::table('quick_user_behavior_monitoring')
                ->where('adsign',$usersign)->where('channel_id',$channel_id)->order('id DESC')
                ->force('usersign')->field('type,advertiser_id,campaign_id,aid,cid')->findOrEmpty();

        if($behavior)
        {
            Db::table('quick_user')->where('id',$uid)->update([
                'advert_media_id' => $behavior['type'], 'advertiser_id' => $behavior['advertiser_id'],
                'aid' => $behavior['aid'], 'cid' => $behavior['cid'], 'campaign_id' => $behavior['campaign_id']
            ]);
        }


    }

}